package com.datamanage.asset.service.impl;

import com.datamanage.asset.entity.Dataset;
import com.datamanage.asset.dto.DatasetDTO;
import com.datamanage.asset.repository.DatasetRepository;
import com.datamanage.asset.service.DatasetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class DatasetServiceImpl implements DatasetService {
    
    @Autowired
    private DatasetRepository datasetRepository;
    
    @Override
    public Dataset createDataset(DatasetDTO datasetDTO, Long userId, Long companyId) {
        Dataset dataset = new Dataset();
        dataset.setName(datasetDTO.getName());
        dataset.setDescription(datasetDTO.getDescription());
        dataset.setDatasetType(datasetDTO.getDatasetType());
        dataset.setFilePath(datasetDTO.getFilePath());
        dataset.setFileSize(datasetDTO.getFileSize());
        dataset.setFileFormat(datasetDTO.getFileFormat());
        dataset.setRowCount(datasetDTO.getRowCount());
        dataset.setColumnCount(datasetDTO.getColumnCount());
        dataset.setTags(datasetDTO.getTags());
        dataset.setVersion(datasetDTO.getVersion());
        dataset.setCreatedBy(userId);
        dataset.setCompanyId(companyId);
        dataset.setCreatedAt(LocalDateTime.now());
        dataset.setUpdatedAt(LocalDateTime.now());
        
        return datasetRepository.save(dataset);
    }
    
    @Override
    public Optional<Dataset> getDatasetById(Long id, Long companyId) {
        return datasetRepository.findByCompanyIdAndId(companyId, id);
    }
    
    @Override
    public Page<Dataset> getAllDatasets(Long companyId, Pageable pageable) {
        return datasetRepository.findByCompanyId(companyId, pageable);
    }
    
    @Override
    public Page<Dataset> getDatasetsByUserId(Long userId, Long companyId, Pageable pageable) {
        return datasetRepository.findByCompanyIdAndUserId(companyId, userId, pageable);
    }
    
    @Override
    public Page<Dataset> getDatasetsByType(String type, Long companyId, Pageable pageable) {
        return datasetRepository.findByCompanyIdAndDatasetType(companyId, type, pageable);
    }
    
    @Override
    public Dataset updateDataset(Long id, DatasetDTO datasetDTO, Long companyId) {
        Optional<Dataset> existingDatasetOpt = datasetRepository.findByCompanyIdAndId(companyId, id);
        if (!existingDatasetOpt.isPresent()) {
            throw new RuntimeException("数据集不存在或无权限访问");
        }
        
        Dataset existingDataset = existingDatasetOpt.get();
        existingDataset.setName(datasetDTO.getName());
        existingDataset.setDescription(datasetDTO.getDescription());
        existingDataset.setFilePath(datasetDTO.getFilePath());
        existingDataset.setFileSize(datasetDTO.getFileSize());
        existingDataset.setFileFormat(datasetDTO.getFileFormat());
        existingDataset.setRowCount(datasetDTO.getRowCount());
        existingDataset.setColumnCount(datasetDTO.getColumnCount());
        existingDataset.setTags(datasetDTO.getTags());
        existingDataset.setVersion(datasetDTO.getVersion());
        existingDataset.setUpdatedAt(LocalDateTime.now());
        
        return datasetRepository.save(existingDataset);
    }
    
    @Override
    public void deleteDataset(Long id, Long companyId) {
        Optional<Dataset> existingDatasetOpt = datasetRepository.findByCompanyIdAndId(companyId, id);
        if (!existingDatasetOpt.isPresent()) {
            throw new RuntimeException("数据集不存在或无权限访问");
        }
        
        Dataset existingDataset = existingDatasetOpt.get();
        existingDataset.setDeleted(true);
        existingDataset.setUpdatedAt(LocalDateTime.now());
        
        datasetRepository.save(existingDataset);
    }
    
    @Override
    public List<Dataset> analyzeDataset(Long id, Long companyId) {
        Optional<Dataset> datasetOpt = datasetRepository.findByCompanyIdAndId(companyId, id);
        if (!datasetOpt.isPresent()) {
            throw new RuntimeException("数据集不存在或无权限访问");
        }
        
        // 这里应该实现实际的数据分析逻辑
        // 目前只是返回示例数据
        Dataset dataset = datasetOpt.get();
        return datasetRepository.findByCreatedByAndIsDeletedFalse(dataset.getCreatedBy())
            .stream()
            .filter(d -> !d.getId().equals(id))
            .collect(Collectors.toList());
    }
}